﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Web;
using System.Net;
using System.IO;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;

//namespace DBManager
//{
public class Util
{
    public Util()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static string TrimVietnameseMark(string str)
    {
        str = str.Replace(".", " ");
        str = str.Replace(",", " ");
        str = str.Replace("_", " ");
        str = str.Replace(";", " ");
        str = str.Replace("?", " ");
        str = str.Replace("(", "");
        str = str.Replace(")", "");
        str = str.Replace("[", "");
        str = str.Replace("]", "");
        str = str.Replace("{", "");
        str = str.Replace("}", "");
        str = str.Replace("<", "");
        str = str.Replace(">", "");
        str = str.Replace("'", "");

        while (str.IndexOf("  ") > 0)
        {
            str = str.Replace("  ", " ");
        }

        str = str.Replace("ấ", "a");
        str = str.Replace("ầ", "a");
        str = str.Replace("ẩ", "a");
        str = str.Replace("ẫ", "a");
        str = str.Replace("ậ", "a");

        str = str.Replace("Ấ", "A");
        str = str.Replace("Ầ", "A");
        str = str.Replace("Ẩ", "A");
        str = str.Replace("Ẫ", "A");
        str = str.Replace("Ậ", "A");

        str = str.Replace("ắ", "a");
        str = str.Replace("ằ", "a");
        str = str.Replace("ẳ", "a");
        str = str.Replace("ẵ", "a");
        str = str.Replace("ặ", "a");

        str = str.Replace("Ắ", "A");
        str = str.Replace("Ằ", "A");
        str = str.Replace("Ẳ", "A");
        str = str.Replace("Ẵ", "A");
        str = str.Replace("Ặ", "A");

        str = str.Replace("á", "a");
        str = str.Replace("à", "a");
        str = str.Replace("ả", "a");
        str = str.Replace("ã", "a");
        str = str.Replace("ạ", "a");
        str = str.Replace("â", "a");
        str = str.Replace("ă", "a");

        str = str.Replace("Á", "A");
        str = str.Replace("À", "A");
        str = str.Replace("Ả", "A");
        str = str.Replace("Ã", "A");
        str = str.Replace("Ạ", "A");
        str = str.Replace("Â", "A");
        str = str.Replace("Ă", "A");

        str = str.Replace("ế", "e");
        str = str.Replace("ề", "e");
        str = str.Replace("ể", "e");
        str = str.Replace("ễ", "e");
        str = str.Replace("ệ", "e");

        str = str.Replace("Ế", "E");
        str = str.Replace("Ề", "E");
        str = str.Replace("Ể", "E");
        str = str.Replace("Ễ", "E");
        str = str.Replace("Ệ", "E");

        str = str.Replace("é", "e");
        str = str.Replace("è", "e");
        str = str.Replace("ẻ", "e");
        str = str.Replace("ẽ", "e");
        str = str.Replace("ẹ", "e");
        str = str.Replace("ê", "e");

        str = str.Replace("É", "E");
        str = str.Replace("È", "E");
        str = str.Replace("Ẻ", "E");
        str = str.Replace("Ẽ", "E");
        str = str.Replace("Ẹ", "E");
        str = str.Replace("Ê", "E");

        str = str.Replace("í", "i");
        str = str.Replace("ì", "i");
        str = str.Replace("ỉ", "i");
        str = str.Replace("ĩ", "i");
        str = str.Replace("ị", "i");

        str = str.Replace("Í", "I");
        str = str.Replace("Ì", "I");
        str = str.Replace("Ỉ", "I");
        str = str.Replace("Ĩ", "I");
        str = str.Replace("Ị", "I");

        str = str.Replace("ố", "o");
        str = str.Replace("ồ", "o");
        str = str.Replace("ổ", "o");
        str = str.Replace("ỗ", "o");
        str = str.Replace("ộ", "o");

        str = str.Replace("Ố", "O");
        str = str.Replace("Ồ", "O");
        str = str.Replace("Ổ", "O");
        str = str.Replace("Ô", "O");
        str = str.Replace("Ộ", "O");

        str = str.Replace("ớ", "o");
        str = str.Replace("ờ", "o");
        str = str.Replace("ở", "o");
        str = str.Replace("ỡ", "o");
        str = str.Replace("ợ", "o");

        str = str.Replace("Ớ", "O");
        str = str.Replace("Ờ", "O");
        str = str.Replace("Ở", "O");
        str = str.Replace("Ỡ", "O");
        str = str.Replace("Ợ", "O");

        str = str.Replace("ứ", "u");
        str = str.Replace("ừ", "u");
        str = str.Replace("ử", "u");
        str = str.Replace("ữ", "u");
        str = str.Replace("ự", "u");

        str = str.Replace("Ứ", "U");
        str = str.Replace("Ừ", "U");
        str = str.Replace("Ử", "U");
        str = str.Replace("Ữ", "U");
        str = str.Replace("Ự", "U");

        str = str.Replace("ý", "y");
        str = str.Replace("ỳ", "y");
        str = str.Replace("ỷ", "y");
        str = str.Replace("ỹ", "y");
        str = str.Replace("ỵ", "y");

        str = str.Replace("Ý", "Y");
        str = str.Replace("Ỳ", "Y");
        str = str.Replace("Ỷ", "Y");
        str = str.Replace("Ỹ", "Y");
        str = str.Replace("Ỵ", "Y");

        str = str.Replace("Đ", "D");
        str = str.Replace("Đ", "D");
        str = str.Replace("đ", "d");

        str = str.Replace("ó", "o");
        str = str.Replace("ò", "o");
        str = str.Replace("ỏ", "o");
        str = str.Replace("õ", "o");
        str = str.Replace("ọ", "o");
        str = str.Replace("ô", "o");
        str = str.Replace("ơ", "o");

        str = str.Replace("Ó", "O");
        str = str.Replace("Ò", "O");
        str = str.Replace("Ỏ", "O");
        str = str.Replace("Õ", "O");
        str = str.Replace("Ọ", "O");
        str = str.Replace("Ô", "O");
        str = str.Replace("Ơ", "O");

        str = str.Replace("ú", "u");
        str = str.Replace("ù", "u");
        str = str.Replace("ủ", "u");
        str = str.Replace("ũ", "u");
        str = str.Replace("ụ", "u");
        str = str.Replace("ư", "u");

        str = str.Replace("Ú", "U");
        str = str.Replace("Ù", "U");
        str = str.Replace("Ủ", "U");
        str = str.Replace("Ũ", "U");
        str = str.Replace("Ụ", "U");
        str = str.Replace("Ư", "U");

        str = str.ToLower();

        return str;
    }
    public static string TrimVietnameseMark(string str, bool isLower)
    {
        str = str.Replace(".", " ");
        str = str.Replace(",", " ");
        str = str.Replace("_", " ");
        str = str.Replace(";", " ");
        str = str.Replace("?", " ");
        str = str.Replace("(", "");
        str = str.Replace(")", "");
        str = str.Replace("[", "");
        str = str.Replace("]", "");
        str = str.Replace("{", "");
        str = str.Replace("}", "");
        str = str.Replace("<", "");
        str = str.Replace(">", "");
        str = str.Replace("'", "");

        while (str.IndexOf("  ") > 0)
        {
            str = str.Replace("  ", " ");
        }

        str = str.Replace("ấ", "a");
        str = str.Replace("ầ", "a");
        str = str.Replace("ẩ", "a");
        str = str.Replace("ẫ", "a");
        str = str.Replace("ậ", "a");

        str = str.Replace("Ấ", "A");
        str = str.Replace("Ầ", "A");
        str = str.Replace("Ẩ", "A");
        str = str.Replace("Ẫ", "A");
        str = str.Replace("Ậ", "A");

        str = str.Replace("ắ", "a");
        str = str.Replace("ằ", "a");
        str = str.Replace("ẳ", "a");
        str = str.Replace("ẵ", "a");
        str = str.Replace("ặ", "a");

        str = str.Replace("Ắ", "A");
        str = str.Replace("Ằ", "A");
        str = str.Replace("Ẳ", "A");
        str = str.Replace("Ẵ", "A");
        str = str.Replace("Ặ", "A");

        str = str.Replace("á", "a");
        str = str.Replace("à", "a");
        str = str.Replace("ả", "a");
        str = str.Replace("ã", "a");
        str = str.Replace("ạ", "a");
        str = str.Replace("â", "a");
        str = str.Replace("ă", "a");

        str = str.Replace("Á", "A");
        str = str.Replace("À", "A");
        str = str.Replace("Ả", "A");
        str = str.Replace("Ã", "A");
        str = str.Replace("Ạ", "A");
        str = str.Replace("Â", "A");
        str = str.Replace("Ă", "A");

        str = str.Replace("ế", "e");
        str = str.Replace("ề", "e");
        str = str.Replace("ể", "e");
        str = str.Replace("ễ", "e");
        str = str.Replace("ệ", "e");

        str = str.Replace("Ế", "E");
        str = str.Replace("Ề", "E");
        str = str.Replace("Ể", "E");
        str = str.Replace("Ễ", "E");
        str = str.Replace("Ệ", "E");

        str = str.Replace("é", "e");
        str = str.Replace("è", "e");
        str = str.Replace("ẻ", "e");
        str = str.Replace("ẽ", "e");
        str = str.Replace("ẹ", "e");
        str = str.Replace("ê", "e");

        str = str.Replace("É", "E");
        str = str.Replace("È", "E");
        str = str.Replace("Ẻ", "E");
        str = str.Replace("Ẽ", "E");
        str = str.Replace("Ẹ", "E");
        str = str.Replace("Ê", "E");

        str = str.Replace("í", "i");
        str = str.Replace("ì", "i");
        str = str.Replace("ỉ", "i");
        str = str.Replace("ĩ", "i");
        str = str.Replace("ị", "i");

        str = str.Replace("Í", "I");
        str = str.Replace("Ì", "I");
        str = str.Replace("Ỉ", "I");
        str = str.Replace("Ĩ", "I");
        str = str.Replace("Ị", "I");

        str = str.Replace("ố", "o");
        str = str.Replace("ồ", "o");
        str = str.Replace("ổ", "o");
        str = str.Replace("ỗ", "o");
        str = str.Replace("ộ", "o");

        str = str.Replace("Ố", "O");
        str = str.Replace("Ồ", "O");
        str = str.Replace("Ổ", "O");
        str = str.Replace("Ô", "O");
        str = str.Replace("Ộ", "O");

        str = str.Replace("ớ", "o");
        str = str.Replace("ờ", "o");
        str = str.Replace("ở", "o");
        str = str.Replace("ỡ", "o");
        str = str.Replace("ợ", "o");

        str = str.Replace("Ớ", "O");
        str = str.Replace("Ờ", "O");
        str = str.Replace("Ở", "O");
        str = str.Replace("Ỡ", "O");
        str = str.Replace("Ợ", "O");

        str = str.Replace("ứ", "u");
        str = str.Replace("ừ", "u");
        str = str.Replace("ử", "u");
        str = str.Replace("ữ", "u");
        str = str.Replace("ự", "u");

        str = str.Replace("Ứ", "U");
        str = str.Replace("Ừ", "U");
        str = str.Replace("Ử", "U");
        str = str.Replace("Ữ", "U");
        str = str.Replace("Ự", "U");

        str = str.Replace("ý", "y");
        str = str.Replace("ỳ", "y");
        str = str.Replace("ỷ", "y");
        str = str.Replace("ỹ", "y");
        str = str.Replace("ỵ", "y");

        str = str.Replace("Ý", "Y");
        str = str.Replace("Ỳ", "Y");
        str = str.Replace("Ỷ", "Y");
        str = str.Replace("Ỹ", "Y");
        str = str.Replace("Ỵ", "Y");

        str = str.Replace("Đ", "D");
        str = str.Replace("Đ", "D");
        str = str.Replace("đ", "d");

        str = str.Replace("ó", "o");
        str = str.Replace("ò", "o");
        str = str.Replace("ỏ", "o");
        str = str.Replace("õ", "o");
        str = str.Replace("ọ", "o");
        str = str.Replace("ô", "o");
        str = str.Replace("ơ", "o");

        str = str.Replace("Ó", "O");
        str = str.Replace("Ò", "O");
        str = str.Replace("Ỏ", "O");
        str = str.Replace("Õ", "O");
        str = str.Replace("Ọ", "O");
        str = str.Replace("Ô", "O");
        str = str.Replace("Ơ", "O");

        str = str.Replace("ú", "u");
        str = str.Replace("ù", "u");
        str = str.Replace("ủ", "u");
        str = str.Replace("ũ", "u");
        str = str.Replace("ụ", "u");
        str = str.Replace("ư", "u");

        str = str.Replace("Ú", "U");
        str = str.Replace("Ù", "U");
        str = str.Replace("Ủ", "U");
        str = str.Replace("Ũ", "U");
        str = str.Replace("Ụ", "U");
        str = str.Replace("Ư", "U");

        if (isLower) str = str.ToLower();

        return str;
    }
    public static string TrimSpecialChar(string str)
    {
        str = TrimVietnameseMark(str);
        str = str.Replace(".", " ");
        str = str.Replace(",", " ");
        str = str.Replace("_", " ");
        str = str.Replace(";", " ");
        str = str.Replace("?", " ");
        str = str.Replace("(", "");
        str = str.Replace(")", "");
        str = str.Replace("[", "");
        str = str.Replace("]", "");
        str = str.Replace("{", "");
        str = str.Replace("}", "");
        str = str.Replace("<", "");
        str = str.Replace(">", "");
        str = str.Replace("'", "");
        //
        str = str.Replace("`", "");
        str = str.Replace("&", "");
        str = str.Replace("^", "");
        str = str.Replace("$", "");
        str = str.Replace("%", "");
        str = str.Replace("#", "");
        str = str.Replace("@", "");
        str = str.Replace("*", "");
        str = str.Replace("-", "");
        while (str.IndexOf("  ") > 0)
        {
            str = str.Replace("  ", " ");
        }
        return str;
    }
    public static string FormatStringSearch(string str)
    {
        //chuan hoa ve k dau k cach
        //bo di cac ki tu la
        return TrimSpecialChar(TrimVietnameseMark(str, true)).Replace(" ", "");
    }
    public static DateTime stringToDateTime(string str)
    {
        DateTime kq = DateTime.ParseExact(str, "yyyyMMddHHmmss", null);
        return kq;
    }
    public static string getVietnameseDateTimeString(string strDatetime)
    {
        if (strDatetime == null || strDatetime == "") return "";
        DateTime dt = DateTime.Parse(strDatetime);
        string strDate = String.Format("{0:dddd, dd MMMM, yyyy hh:mm}", dt);

        strDate = strDate.Replace("Sunday", "Chủ nhật");
        strDate = strDate.Replace("Monday", "Thứ hai");
        strDate = strDate.Replace("Tuesday", "Thứ ba");
        strDate = strDate.Replace("Wenesday", "Thứ tư");
        strDate = strDate.Replace("Thursday", "Thứ năm");
        strDate = strDate.Replace("Friday", "Thứ sáu");
        strDate = strDate.Replace("Saturday", "Thứ bảy");

        strDate = strDate.Replace("January", "Tháng 1");
        strDate = strDate.Replace("February", "Tháng 2");
        strDate = strDate.Replace("March", "Tháng 3");
        strDate = strDate.Replace("April", "Tháng 4");
        strDate = strDate.Replace("May", "Tháng 5");
        strDate = strDate.Replace("June", "Tháng 6");
        strDate = strDate.Replace("July", "Tháng 7");
        strDate = strDate.Replace("August", "Tháng 8");
        strDate = strDate.Replace("September", "Tháng 9");
        strDate = strDate.Replace("October", "Tháng 10");
        strDate = strDate.Replace("November", "Tháng 11");
        strDate = strDate.Replace("December", "Tháng 12");

        return strDate;
    }
    public static string getHiddenTel(string sTel)
    {
        try
        {
            if (sTel.StartsWith("+84")) sTel = sTel.Substring(0, sTel.Length - 6) + "**" + sTel.Substring(sTel.Length - 4);
            return sTel;
        }
        catch { return sTel; }
    }
    /// <summary>
    /// cat cau
    /// </summary>
    /// <param name="length"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string cutSentence(int length, string str)
    {
        try
        {
            if (str.Length > length)
            {
                str = str.Substring(0, length);
                if (str.LastIndexOf(" ") != -1) str = str.Substring(0, str.LastIndexOf(" ")) + " ...";
            }
        }
        catch { }
        return str;
    }
    public static string getUrlContent(string lcUrl)
    {
        // string lcUrl = "http://www.west-wind.com/TestPage.wwd";



        // *** Establish the request

        HttpWebRequest loHttp =

             (HttpWebRequest)WebRequest.Create(lcUrl);



        // *** Set properties

        loHttp.Timeout = 10000;     // 10 secs

        loHttp.UserAgent = "Code Sample Web Client";



        // *** Retrieve request info headers

        HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();



        Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page



        StreamReader loResponseStream =

           new StreamReader(loWebResponse.GetResponseStream(), enc);



        string lcHtml = loResponseStream.ReadToEnd();



        loWebResponse.Close();

        loResponseStream.Close();
        return lcHtml;

    }
    public static string getTelToSend(string tel)
    {
        if (tel.StartsWith("+84"))
        {
            return tel;
        }
        if (tel.StartsWith("+084"))
        {
            return tel;
        }
        if (tel.StartsWith("0")) return "+84" + tel.Substring(1);
        return "+84" + tel;
    }
    public static string getTelToCompare(string tel)
    {
        if (tel.StartsWith("+84"))
        {
            return tel.Replace("+84", "");
        }
        if (tel.StartsWith("+084"))
        {
            return tel.Replace("+084", "");
        }
        if (tel.StartsWith("0"))
        {
            return tel.Substring(1);
        }
        return tel;
    }
    public static string getTelToView(string tel)
    {
        if (tel.StartsWith("+84"))
        {
            return tel.Replace("+84", "0");
        }
        if (tel.StartsWith("+084"))
        {
            return tel.Replace("+084", "0");
        }
        return tel;
    }
    public static bool hasRecordOnline()
    {
        //test
        //return true; 

        string sDirOnline = ConfigurationManager.AppSettings["dirRecordOnline"];
        if (string.IsNullOrEmpty(sDirOnline))
        {
            sDirOnline = "C:\\";
        }
        if (!sDirOnline.EndsWith("\\")) sDirOnline += "\\";
        object sUser = HttpContext.Current.Session["user"];
        return File.Exists(sDirOnline + "" + getTelToView(sUser.ToString()) + ".flv");
    }
    public static int saveRecordOnline()
    {
        try
        {
            object sUser = HttpContext.Current.Session["user"];
            if (sUser == null || sUser.ToString() == "") return 0;

            string sFileName = "" + getTelToView(sUser.ToString()) + ".flv";

            return M4MEUtil.updateGif(sUser.ToString(), new string[] { "", "", "3", sFileName });
            //--------------------

            object memberid = DBUtil.ExecuteScalar("Select top 1 id from members where tel=" + comm.to_sql(sUser.ToString(), "text") + "");

            string sqls = "SELECT G.* FROM comp_componentGif G,Members M,Component_Members CM WHERE M.Tel=" + comm.to_sql(sUser.ToString(), "text") + " AND M.Id=CM.memberid AND G.Id=CM.componentGifId AND G.Type=3 AND G.Memberid=" + comm.to_sqlnumber(memberid.ToString()) + "";
            //Nếu có rồi thì không chèn thêm nữa
            DataTable dt = DBUtil.SelectTable(sqls);
            if (dt.Rows.Count > 0) return 1;

            //Nếu chưa có thì chèn mới 1 comp cho nó

            //Kiểm tra xem nó đã sử dụng comp gif nào chưa
            string sqlc = "select componentId from Component_Members CM,Members M,Components C where C.id=CM.ComponentID AND C.Type='Gift' AND M.Id=CM.memberid AND M.tel=" + comm.to_sql(sUser.ToString(), "text") + " AND CM.active=1";

            object compId = DBUtil.ExecuteScalar(sqlc);


            //Nếu chưa có thì cho nó comp mặc định
            // Sau đó thêm các thuộc tính vào comp mặc định
            string componentDefaultGift = M4MEUtil.getIDGifDefault();
            if (compId == null)
            {
                string sqlin = "INSERT INTO comp_ComponentGif(textContent,[type],memberid,componentid)"
                + " VALUES(" + comm.to_sql("" + getTelToView(sUser.ToString()) + ".flv", "text")
                + ",3," + memberid + "," + componentDefaultGift + ") SELECT @@IDENTITY ";

                object gifid = DBUtil.ExecuteScalar(sqlin);


                string sqlcm = "IF NOT EXISTS(select * from Component_Members where memberid =" + comm.to_sqlnumber(memberid.ToString()) + " and ComponentId=" + comm.to_sqlnumber(componentDefaultGift) + ") BEGIN INSERT INTO Component_Members(memberid,componentid,componentGifId,active) VALUES "
                    + "(" + comm.to_sqlnumber(memberid.ToString()) + "," + componentDefaultGift + "," + comm.to_sqlnumber(gifid.ToString()) + ",1) END ;";
                int nx = DBUtil.Execute(sqlcm);

                if (nx <= 0)
                {
                    sqlcm = " Update Component_Members set active=1 where memberid =" + comm.to_sqlnumber(memberid.ToString()) + " and ComponentId=" + comm.to_sqlnumber(componentDefaultGift) + " ";
                    nx = DBUtil.Execute(sqlcm);
                }

            }
            //them record nay vao comp gif no dang dung
            else
            {
                string sqlin = "INSERT INTO comp_ComponentGif(textContent,[type],memberid,componentid)"
                + " VALUES("
                + comm.to_sql("" + getTelToView(sUser.ToString()) + ".flv", "text")
                + ",3," + comm.to_sqlnumber(memberid.ToString()) + "," + compId.ToString() + ")";

                //object gifid = DBUtil.ExecuteScalar(sqlin);
                DBUtil.Execute(sqlin);
            }


            //string sql = "INSERT INTO comp_ComponentGif([type],memberid,textContent)VALUES("
            //    + "3,"
            //    + comm.to_sqlnumber(memberid.ToString()) + ","
            //    + comm.to_sql("" + getTelToView(sUser.ToString()) + ".flv", "text")
            //    + ") SELECT @@IDENTITY ";
            //object gifId = DBUtil.ExecuteScalar(sql);
            //object memberId = DBUtil.ExecuteScalar("SELECT top 1 ID FROM Members WHERE Tel=" + comm.to_sql(getTelToSend(sUser.ToString()),"text") + " ");


            //string sqlCM = "INSERT INTO Component_Members(componentGifId,memberid)VALUES("+gifId.ToString()+","+memberid+")";

            //return DBUtil.Execute(sqlCM);
        }
        catch (Exception ex) { return -1; }
        return 1;
    }
    public static void logM4ME(string sContent)
    {
        try
        {
            using (StreamWriter str = new StreamWriter("C:\\log\\logM4ME" + DateTime.Now.ToString("yyyyMMdd") + ".txt", true))
            {
                str.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + ":" + sContent);
            }
        }
        catch { }
    }
    public static void logExcuteSql(string sContent)
    {
        try
        {
            using (StreamWriter str = new StreamWriter("C:\\log\\ExcuteSql" + DateTime.Now.ToString("yyyyMMdd") + ".txt", true))
            {
                str.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt") + ":" + sContent);
            }
        }
        catch { }
    }
    public static bool deleteRecordOnline()
    {
        string sDirOnline = ConfigurationManager.AppSettings["dirRecordOnline"];
        try
        {
            if (string.IsNullOrEmpty(sDirOnline))
            {
                sDirOnline = "C:\\";
            }
            if (!sDirOnline.EndsWith("\\")) sDirOnline += "\\";

            object sUser = HttpContext.Current.Session["user"];

            //string sql = "DELETE FROM comp_componentGif WHERE Id IN(SELECT G.ID FROM Comp_ComponentGif G,Members M,Component_Members CM WHERE M.Tel=" + comm.to_sql(sUser.ToString(), "text") + " AND M.Id=CM.memberid AND G.Id=CM.componentGifId AND G.Type=3)";
            object memberid = DBUtil.ExecuteScalar("Select top 1 id from members where tel=" + comm.to_sql(sUser.ToString(), "text") + "");

            string sql = "DELETE FROM comp_componentGif WHERE Memberid=" + comm.to_sqlnumber(memberid.ToString()) + " AND [Type]=3 ";

            DBUtil.Execute(sql);

            //if (File.Exists(sDirOnline + "" + getTelToView(sUser.ToString()) + ".flv"))
            //{
            //    File.Delete(sDirOnline + "" + getTelToView(sUser.ToString()) + ".flv");

            //}


            return true;
        }
        catch (Exception ex) { logM4ME("Exception when deleteRecord:" + ex.ToString()); return false; }

    }
    /// <summary>
    /// Uploads the file.
    /// </summary>
    /// <param name="fUpload">The upload control .</param>
    /// <param name="fPath">The path string.</param>
    public static string UploadFile(FileUpload fUpload, string type)
    {
        if (type.ToLower() == "swf" && !fUpload.FileName.EndsWith(".swf", StringComparison.OrdinalIgnoreCase)) return "";
        if (type.ToLower() == "images" && !IsFileImage(fUpload.FileName)) return "";

        string fPath = String.Empty;
        string path = GetPathUpload(type);
        try
        {
            fPath = HttpContext.Current.Server.MapPath(GetPathUpload(type));
            //ConfigurationManager.AppSettings["dirUploadGif"]; //
            //if (!fPath.EndsWith("\\")) fPath += "\\";

            //if (type != "")
            //{
            //    fPath += type + "/";
            //    path += type + "/";
            //}
            string sDirTarget = fPath;
            fPath += fUpload.FileName;

            path += fUpload.FileName;



            if (!Directory.Exists(sDirTarget)) Directory.CreateDirectory(sDirTarget);

            FileInfo fInfo = new FileInfo(fPath);
            if (fInfo.Exists)
            {
                fInfo.Delete();
            }
            fUpload.SaveAs(fPath);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        //return fPath.Substring(fPath.Replace("\\","/").IndexOf("/admin"));
        return path;
    }

    /// <summary>
    /// Uploads the file and rename.
    /// </summary>
    /// <param name="fUpload">The upload control .</param>
    /// <param name="fPath">The path string.</param>
    public static string UploadFileWithUniqueFileName(FileUpload fUpload, string type)
    {
        if (type.ToLower() == "swf" && !fUpload.FileName.EndsWith(".swf", StringComparison.OrdinalIgnoreCase)) return "";
        if (type.ToLower() == "images" && !IsFileImage(fUpload.FileName)) return "";

        string fPath = String.Empty;//path vat ly
        string path = GetPathUpload(type);//path luu vao csdl
        try
        {
            fPath = HttpContext.Current.Server.MapPath(GetPathUpload(type));

            string sDirTarget = fPath;
            string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss") + "_";

            fPath += timestamp + fUpload.FileName;

            path += timestamp + fUpload.FileName;



            if (!Directory.Exists(sDirTarget)) Directory.CreateDirectory(sDirTarget);

            FileInfo fInfo = new FileInfo(fPath);
            if (fInfo.Exists)
            {
                fInfo.Delete();
            }
            fUpload.SaveAs(fPath);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        //return fPath.Substring(fPath.Replace("\\","/").IndexOf("/admin"));
        return path;
    }
    /// <summary>
    /// Uploads the file and create thumb file.
    /// </summary>
    /// <param name="fUpload">The upload control .</param>
    /// <param name="fPath">The path string.</param>
    public static string[] UploadImageFileAndCreateThumb(FileUpload fUpload, int maxSizeThumb, string type)
    {
        //if (type.ToLower() == "swf" && !fUpload.FileName.EndsWith(".swf", StringComparison.OrdinalIgnoreCase)) return "";
        //if (type.ToLower() == "images" && !IsFileImage(fUpload.FileName)) return "";

        string fPath = String.Empty;//path vat ly
        string path = GetPathUpload(type);//path luu vao csdl
        string pathThumb = path;

        try
        {
            fPath = HttpContext.Current.Server.MapPath(GetPathUpload(type));

            string sDirTarget = fPath;
            string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss") + "_";

            fPath += timestamp + fUpload.FileName;

            path += timestamp + fUpload.FileName;



            if (!Directory.Exists(sDirTarget)) Directory.CreateDirectory(sDirTarget);

            FileInfo fInfo = new FileInfo(fPath);
            if (fInfo.Exists)
            {
                fInfo.Delete();
            }
            fUpload.SaveAs(fPath);


            //create thumbnail
            /* Đọc ảnh vừa up vào đối tương image để xử lý */
            System.Drawing.Image fullSizeImg;
            fullSizeImg = System.Drawing.Image.FromFile(fPath);

            int imageHeightOrigin = fullSizeImg.Height;
            int imageWidthOrigin = fullSizeImg.Width;

            int widthThumb = 300;
            int heightThumb = 300;

            if (imageHeightOrigin > imageWidthOrigin)
            {
                heightThumb = maxSizeThumb;
                widthThumb = imageWidthOrigin * maxSizeThumb / imageHeightOrigin;
            }
            else
            {
                widthThumb = maxSizeThumb;
                heightThumb = imageHeightOrigin * maxSizeThumb / imageWidthOrigin;
            }

            System.Drawing.Bitmap DestImage = new System.Drawing.Bitmap(widthThumb, heightThumb, fullSizeImg.PixelFormat);

            pathThumb += timestamp + "_" + maxSizeThumb.ToString() + "_thumb_" + fUpload.FileName;

            string pathSave = sDirTarget + "/" + timestamp + "_" + maxSizeThumb.ToString() + "_thumb_" + fUpload.FileName;



            Graphics g = Graphics.FromImage(DestImage);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

            //Rectangle src = new Rectangle(0, 0, fx, fy);
            //Rectangle desc = new Rectangle(0, 0, w, h);

            g.DrawImage(fullSizeImg, 0, 0, widthThumb, heightThumb);

            //target.Save("C:\\abc.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            //post anh len file server
            if (path != "" && pathThumb != "")
            {
                try
                {
                    //path = files.postFileToFileServer(HttpContext.Current.Server.MapPath(path), timestamp + fUpload.FileName);
                    //pathThumb = files.postFileToFileServer(HttpContext.Current.Server.MapPath(pathThumb), timestamp + "_" + maxSizeThumb.ToString() + "_thumb_" + fUpload.FileName);
                }
                catch { }
            }

            DestImage.Save(pathSave);

            return new string[] { path, pathThumb };

        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
        //return fPath.Substring(fPath.Replace("\\","/").IndexOf("/admin"));
        return new string[] { path, pathThumb };
    }
    public static bool IsFileImage(string p)
    {
        return p.ToLower().EndsWith(".jpg") || p.ToLower().EndsWith(".jpeg") || p.ToLower().EndsWith(".bmp") || p.ToLower().EndsWith(".png") || p.ToLower().EndsWith(".gif");
    }

    public static string GetPathUpload(string type)
    {
        if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["pathUpload" + type])) return ConfigurationManager.AppSettings["pathUpload" + type];
        return "/upload/" + type + "/";
    }
    /// <summary>
    /// Them thuoc tinh cho Comp
    /// </summary>
    /// <param name="componentid"></param>
    /// <param name="arrPro">Title,path,posX,posY,w,h,desc,typeComponent,idflash,point,isview,isauto,autoid</param>
    /// <returns></returns>
    public static int addProForComp(string[] arrPro)
    {
        string sTitle = arrPro[0];
        string sPath = arrPro[1];
        string sPosX = arrPro[2];
        string sPosY = arrPro[3];
        string sW = arrPro[4];
        string sH = arrPro[5];
        string sDesc = arrPro[6];
        string sTypeComponent = arrPro[7];
        string sIdFlash = arrPro[8];
        string sPoint = arrPro[9];
        string sIsView = arrPro[10];
        string sIsAuto = arrPro[11];
        string sAutoId = arrPro[12];

        string sqlins = "INSERT INTO Comp_ComponentGif(Title,path,posX,posY,w,h,[desc],typeComponent,idflash,point,isview,isauto,autoid) VALUES("
            + comm.to_sqltext(sTitle) + ","
            + comm.to_sqltext(sPath) + ","
            + comm.to_sqlnumber(sPosX) + ","
            + comm.to_sqlnumber(sPosY) + ","
            + comm.to_sqlnumber(sW) + ","
            + comm.to_sqlnumber(sH) + ","
            + comm.to_sqltext(sDesc) + ","
            + comm.to_sqlnumber(sTypeComponent) + ","
            + comm.to_sqltext(sIdFlash) + ","
            + comm.to_sqlnumber(sPoint) + ","
            + comm.to_sqlnumber(sIsView) + ","
            + comm.to_sqlnumber(sIsAuto) + ","
            + comm.to_sqlnumber(sAutoId) + ""
              + ")";


        return DBUtil.Execute(sqlins);
    }
    /// <summary>
    /// Lấy nickname theo memberid
    /// </summary>
    /// <returns></returns>
    public static string getMemberNameById(string memberid)
    {
        string sql = "select Tel,[name] from Members where id=" + comm.to_sqlnumber(memberid) + "";
        DataTable dt = DBUtil.SelectTable(sql);

        if (dt.Rows.Count > 0)
        {
            if (!dt.Rows[0]["Name"].ToString().StartsWith("+84")) return dt.Rows[0]["Name"].ToString();
            else return Util.getHiddenTel(dt.Rows[0]["Tel"].ToString());
        }

        return "";
    }
    /// <summary>
    /// Lấy memberid theo nickname
    /// </summary>
    /// <returns></returns>
    public static string getMemberIdByNickName(string nickname)
    {
        string sql = "select Tel,[name] from Members where [name]=" + comm.to_sqltext(nickname) + "";
        DataTable dt = DBUtil.SelectTable(sql);

        if (dt.Rows.Count > 0)
        {
            return dt.Rows[0]["Id"].ToString();

        }
        return "0";
    }
    public static string replaceStringToSmiley(string message)
    {

        message = message.Replace(">:D<", "<img align='absmiddle'  src='/images/Icons/6.gif' alt='big hug' border='0' height='18' width='42'>");
        message = message.Replace(">:d<", "<img align='absmiddle'  src='/images/Icons/6.gif' alt='big hug' border='0' height='18' width='42'>");
        message = message.Replace(":-#", "<img align='absmiddle'  src='/images/Icons/31.gif' alt='sick' border='0' height='18' width='18'>");
        message = message.Replace("<:-P", "<img align='absmiddle'  src='/images/Icons/36.gif' alt='party' border='0' height='18' width='38'>");
        message = message.Replace("<:-p", "<img align='absmiddle'  src='/images/Icons/36.gif' alt='party' border='0' height='18' width='38'>");
        message = message.Replace("<):)", "<img align='absmiddle'  src='/images/Icons/48.gif' alt='cowboy' border='0' height='18' width='18'>");
        message = message.Replace(":\">", "<img align='absmiddle'  src='/images/Icons/9.gif' alt='blushing' border='0' height='18' width='18'>");
        message = message.Replace(">:)", "<img align='absmiddle'  src='/images/Icons/19.gif' alt='devil' border='0' height='18' width='18'>");
        message = message.Replace("8->", "<img align='absmiddle'  src='/images/Icons/105.gif' alt='daydreaming - New!' border='0' height='18' width='23'>");
        message = message.Replace("=D>", "<img align='absmiddle'  src='/images/Icons/41.gif' alt='applause' border='0' height='18' width='18'>");
        message = message.Replace("=d>", "<img align='absmiddle'  src='/images/Icons/41.gif' alt='applause' border='0' height='18' width='18'>");
        message = message.Replace(":-<", "<img align='absmiddle'  src='/images/Icons/46.gif' alt='sigh' border='0' height='18' width='24'>");
        message = message.Replace(">:P", "<img align='absmiddle'  src='/images/Icons/47.gif' alt='phbbbbt' border='0' height='18' width='18'>");
        message = message.Replace(">:p", "<img align='absmiddle'  src='/images/Icons/47.gif' alt='phbbbbt' border='0' height='18' width='18'>");
        message = message.Replace(":>", "<img align='absmiddle'  src='/images/Icons/15.gif' alt='smug' border='0' height='18' width='18'>");
        message = message.Replace("#:-S", "<img align='absmiddle'  src='/images/Icons/18.gif' alt='whew!' border='0' height='18' width='34'>");
        message = message.Replace("#:-s", "<img align='absmiddle'  src='/images/Icons/18.gif' alt='whew!' border='0' height='18' width='34'>");
        message = message.Replace("O:-)", "<img align='absmiddle'  src='/images/Icons/25.gif' alt='angel' border='0' height='18' width='30'>");
        message = message.Replace(":-SS", "<img align='absmiddle'  src='/images/Icons/42.gif' alt='nailbiting' border='0' height='18' width='36'>");
        message = message.Replace(":-ss", "<img align='absmiddle'  src='/images/Icons/42.gif' alt='nailbiting' border='0' height='18' width='36'>");
        message = message.Replace(";;)", "<img align='absmiddle'  src='/images/Icons/5.gif' alt='batting eyelashes' border='0' height='18' width='18'>");
        message = message.Replace(":-/", "<img align='absmiddle'  src='/images/Icons/7.gif' alt='confused' border='0' height='18' width='20'>");
        message = message.Replace(":-*", "<img align='absmiddle'  src='/images/Icons/11.gif' alt='kiss' border='0' height='18' width='18'>");
        message = message.Replace("=((", "<img align='absmiddle'  src='/images/Icons/12.gif' alt='broken heart' border='0' height='18' width='18'>");
        message = message.Replace(":-O", "<img align='absmiddle'  src='/images/Icons/13.gif' alt='surprise' border='0' height='18' width='18'>");
        message = message.Replace(":-o", "<img align='absmiddle'  src='/images/Icons/13.gif' alt='surprise' border='0' height='18' width='18'>");
        message = message.Replace("B-)", "<img align='absmiddle'  src='/images/Icons/16.gif' alt='cool' border='0' height='18' width='18'>");
        message = message.Replace("b-)", "<img align='absmiddle'  src='/images/Icons/16.gif' alt='cool' border='0' height='18' width='18'>");
        message = message.Replace(":-S", "<img align='absmiddle'  src='/images/Icons/17.gif' alt='worried' border='0' height='18' width='18'>");
        message = message.Replace(":-s", "<img align='absmiddle'  src='/images/Icons/17.gif' alt='worried' border='0' height='18' width='18'>");
        message = message.Replace(":((", "<img align='absmiddle'  src='/images/Icons/20.gif' alt='crying' border='0' height='18' width='22'>");
        message = message.Replace(":))", "<img align='absmiddle'  src='/images/Icons/21.gif' alt='laughing' border='0' height='18' width='18'>");
        message = message.Replace("/:)", "<img align='absmiddle'  src='/images/Icons/23.gif' alt='raised eyebrow' border='0' height='18' width='18'>");
        message = message.Replace("=))", "<img align='absmiddle'  src='/images/Icons/24.gif' alt='rolling on the floor' border='0' height='18' width='30'>");
        message = message.Replace(":-B", "<img align='absmiddle'  src='/images/Icons/26.gif' alt='nerd' border='0' height='18' width='24'>");
        message = message.Replace(":-b", "<img align='absmiddle'  src='/images/Icons/26.gif' alt='nerd' border='0' height='18' width='24'>");
        message = message.Replace(":-C", "<img align='absmiddle'  src='/images/Icons/101.gif' alt='call me - New!' border='0' height='18' width='28'>");
        message = message.Replace(":-c", "<img align='absmiddle'  src='/images/Icons/101.gif' alt='call me - New!' border='0' height='18' width='28'>");
        message = message.Replace(":)]", "<img align='absmiddle'  src='/images/Icons/100.gif' alt='on the phone - New!' border='0' height='18' width='31'>");
        message = message.Replace("~X(", "<img align='absmiddle'  src='/images/Icons/102.gif' alt='at wits\' end - New!' border='0' height='18' width='44'>");
        message = message.Replace("~x(", "<img align='absmiddle'  src='/images/Icons/102.gif' alt='at wits\' end - New!' border='0' height='18' width='44'>");
        message = message.Replace(":-H", "<img align='absmiddle'  src='/images/Icons/103.gif' alt='wave - New!' border='0' height='18' width='28'>");
        message = message.Replace(":-h", "<img align='absmiddle'  src='/images/Icons/103.gif' alt='wave - New!' border='0' height='18' width='28'>");
        message = message.Replace(":-T", "<img align='absmiddle'  src='/images/Icons/104.gif' alt='time out - New!' border='0' height='18' width='30'>");
        message = message.Replace(":-t", "<img align='absmiddle'  src='/images/Icons/104.gif' alt='time out - New!' border='0' height='18' width='30'>");
        message = message.Replace("I-)", "<img align='absmiddle'  src='/images/Icons/28.gif' alt='sleepy' border='0' height='18' width='21'>");
        message = message.Replace("i-)", "<img align='absmiddle'  src='/images/Icons/28.gif' alt='sleepy' border='0' height='18' width='21'>");
        message = message.Replace("8-|", "<img align='absmiddle'  src='/images/Icons/29.gif' alt='rolling eyes' border='0' height='18' width='18'>");
        message = message.Replace("L-)", "<img align='absmiddle'  src='/images/Icons/30.gif' alt='loser' border='0' height='18' width='24'>");
        message = message.Replace("l-)", "<img align='absmiddle'  src='/images/Icons/30.gif' alt='loser' border='0' height='18' width='24'>");
        message = message.Replace(":-$", "<img align='absmiddle'  src='/images/Icons/32.gif' alt='don\'t tell anyone' border='0' height='18' width='18'>");
        message = message.Replace("[-(", "<img align='absmiddle'  src='/images/Icons/33.gif' alt='not talking' border='0' height='18' width='18'>");
        message = message.Replace(":O)", "<img align='absmiddle'  src='/images/Icons/34.gif' alt='clown' border='0' height='18' width='28'>");
        message = message.Replace(":o)", "<img align='absmiddle'  src='/images/Icons/34.gif' alt='clown' border='0' height='18' width='28'>");
        message = message.Replace("8-}", "<img align='absmiddle'  src='/images/Icons/35.gif' alt='silly' border='0' height='18' width='24'>");
        message = message.Replace("(:|", "<img align='absmiddle'  src='/images/Icons/37.gif' alt='yawn' border='0' height='18' width='18'>");
        message = message.Replace("=P~", "<img align='absmiddle'  src='/images/Icons/38.gif' alt='drooling' border='0' height='18' width='18'>");
        message = message.Replace("=p~", "<img align='absmiddle'  src='/images/Icons/38.gif' alt='drooling' border='0' height='18' width='18'>");
        message = message.Replace(":-?", "<img align='absmiddle'  src='/images/Icons/39.gif' alt='thinking' border='0' height='18' width='18'>");
        message = message.Replace("#-O", "<img align='absmiddle'  src='/images/Icons/40.gif' alt='d\'oh' border='0' height='18' width='24'>");
        message = message.Replace("#-o", "<img align='absmiddle'  src='/images/Icons/40.gif' alt='d\'oh' border='0' height='18' width='24'>");
        message = message.Replace("@-)", "<img align='absmiddle'  src='/images/Icons/43.gif' alt='hypnotized' border='0' height='18' width='18'>");
        message = message.Replace(":^O", "<img align='absmiddle'  src='/images/Icons/44.gif' alt='liar' border='0' height='18' width='18'>");
        message = message.Replace(":^o", "<img align='absmiddle'  src='/images/Icons/44.gif' alt='liar' border='0' height='18' width='18'>");
        message = message.Replace(":-W", "<img align='absmiddle'  src='/images/Icons/45.gif' alt='waiting' border='0' height='18' width='23'>");
        message = message.Replace(":-w", "<img align='absmiddle'  src='/images/Icons/45.gif' alt='waiting' border='0' height='18' width='23'>");
        message = message.Replace(":)", "<img align='absmiddle'  src='/images/Icons/1.gif' alt='happy' border='0' height='18' width='18'>");
        message = message.Replace(":(", "<img align='absmiddle'  src='/images/Icons/2.gif' alt='sad' border='0' height='18' width='18'>");
        message = message.Replace(";)", "<img align='absmiddle'  src='/images/Icons/3.gif' alt='winking' border='0' height='18' width='18'>");
        message = message.Replace(":D", "<img align='absmiddle'  src='/images/Icons/4.gif' alt='big grin' border='0' height='18' width='18'>");
        message = message.Replace(":d", "<img align='absmiddle'  src='/images/Icons/4.gif' alt='big grin' border='0' height='18' width='18'>");
        message = message.Replace(":X", "<img align='absmiddle'  src='/images/Icons/8.gif' alt='love struck' border='0' height='18' width='18'>");
        message = message.Replace(":x", "<img align='absmiddle'  src='/images/Icons/8.gif' alt='love struck' border='0' height='18' width='18'>");
        message = message.Replace(":P", "<img align='absmiddle'  src='/images/Icons/10.gif' alt='tongue' border='0' height='18' width='18'>");
        message = message.Replace(":p", "<img align='absmiddle'  src='/images/Icons/10.gif' alt='tongue' border='0' height='18' width='18'>");
        message = message.Replace("X(", "<img align='absmiddle'  src='/images/Icons/14.gif' alt='angry' border='0' height='18' width='34'>");
        message = message.Replace("x(", "<img align='absmiddle'  src='/images/Icons/14.gif' alt='angry' border='0' height='18' width='34'>");
        message = message.Replace(":|", "<img align='absmiddle'  src='/images/Icons/22.gif' alt='straight face' border='0' height='18' width='18'>");
        message = message.Replace("=;", "<img align='absmiddle'  src='/images/Icons/27.gif' alt='talk to the hand' border='0' height='18' width='18'>");

        return message;
    }
}
//}
